<!DOCTYPE html>
<html>
<head>
<title>Physics Diagram of a Chain</title>
</head>
<body>
<canvas id="physicsCanvas" width="400" height="450" style="border:1px solid #000;"></canvas>
<script>
    const canvas = document.getElementById('physicsCanvas');
    const ctx = canvas.getContext('2d');

    // --- Style and Coordinate Definitions ---
    ctx.lineWidth = 2;
    ctx.strokeStyle = 'black';
    ctx.fillStyle = 'black';
    ctx.textAlign = 'center';
    ctx.textBaseline = 'middle';

    const centerX = 200;
    const tableY = 350;
    const chainHeight = 200;
    const chainTopY = tableY - chainHeight;
    const forceArrowLength = 70;
    const forceArrowTipY = chainTopY - forceArrowLength;
    const dimLineX = 280;

    // --- 1. Draw the Table ---
    ctx.beginPath();
    ctx.moveTo(50, tableY);
    ctx.lineTo(350, tableY);
    // Table legs
    ctx.moveTo(50, tableY);
    ctx.lineTo(50, tableY + 15);
    ctx.moveTo(350, tableY);
    ctx.lineTo(350, tableY + 15);
    ctx.stroke();

    // --- 2. Draw the Piled Chain on the Table ---
    ctx.save();
    ctx.lineWidth = 4;
    // First layer of the pile
    ctx.beginPath();
    ctx.moveTo(centerX - 60, tableY);
    ctx.bezierCurveTo(centerX - 40, tableY - 15, centerX - 20, tableY - 15, centerX, tableY);
    ctx.bezierCurveTo(centerX + 20, tableY - 15, centerX + 40, tableY - 15, centerX + 60, tableY);
    ctx.stroke();
    // Second, slightly smaller layer
    ctx.beginPath();
    ctx.moveTo(centerX - 50, tableY);
    ctx.bezierCurveTo(centerX - 30, tableY - 10, centerX - 10, tableY - 10, centerX + 10, tableY);
    ctx.bezierCurveTo(centerX + 30, tableY - 10, centerX + 50, tableY - 10, centerX + 70, tableY);
    ctx.stroke();
    ctx.restore();

    // --- 3. Draw the Vertical Hanging Chain ---
    ctx.save();
    ctx.lineWidth = 2.5;
    ctx.beginPath();
    ctx.moveTo(centerX, tableY);
    let currentY = tableY;
    const segmentLength = 8;
    const wobble = 3;
    let direction = 1;
    while (currentY > chainTopY) {
        const nextY = Math.max(currentY - segmentLength, chainTopY);
        const midY = (currentY + nextY) / 2;
        ctx.quadraticCurveTo(centerX + wobble * direction, midY, centerX, nextY);
        currentY = nextY;
        direction *= -1; // Alternate sides for a crinkly effect
    }
    ctx.stroke();
    ctx.restore();

    // --- 4. Draw the Upward Force F ---
    // Arrow line
    ctx.beginPath();
    ctx.moveTo(centerX, chainTopY);
    ctx.lineTo(centerX, forceArrowTipY);
    ctx.stroke();
    // Arrowhead
    ctx.beginPath();
    ctx.moveTo(centerX, forceArrowTipY);
    ctx.lineTo(centerX - 6, forceArrowTipY + 10);
    ctx.moveTo(centerX, forceArrowTipY);
    ctx.lineTo(centerX + 6, forceArrowTipY + 10);
    ctx.stroke();
    // Label 'F'
    ctx.font = 'italic bold 20px serif';
    ctx.fillText('F', centerX, forceArrowTipY - 15);

    // --- 5. Draw the Dimension Line for Height h ---
    // Dashed line from chain top to dimension line
    ctx.save();
    ctx.setLineDash([5, 5]);
    ctx.beginPath();
    ctx.moveTo(centerX + 2, chainTopY); // +2 to clear the chain line
    ctx.lineTo(dimLineX, chainTopY);
    ctx.stroke();
    ctx.restore();

    // Vertical dimension line
    ctx.beginPath();
    ctx.moveTo(dimLineX, chainTopY);
    ctx.lineTo(dimLineX, tableY);
    // Top T-bar
    ctx.moveTo(dimLineX - 8, chainTopY);
    ctx.lineTo(dimLineX + 8, chainTopY);
    // Bottom T-bar
    ctx.moveTo(dimLineX - 8, tableY);
    ctx.lineTo(dimLineX + 8, tableY);
    ctx.stroke();
    // Label 'h'
    ctx.font = 'italic bold 20px serif';
    ctx.fillText('h', dimLineX + 20, (chainTopY + tableY) / 2);

    // --- 6. Draw the Figure Label ---
    ctx.font = 'bold 18px serif';
    ctx.fillText('Fig. 1.103.', centerX, 420);
</script>
</body>
</html>